/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import util.J2meAppHelper;
import util.NumeroTelefoneItem;

/**
 *
 * @author Alexandre Pretyman
 */
public class DiscarForm extends Form implements AppForm, CommandListener, ItemCommandListener {

    private Command cmdBack = new Command("Voltar", Command.BACK, 0);
    private Command dialCmd = new Command("Discar", Command.ITEM, 1);
    private Command operCmd = new Command("Interurbano", Command.ITEM, 2);
    private StringItem nomeStrItm = new StringItem("", "");
    private StringItem phoneLbl = new StringItem("", "");
    private NumeroTelefoneItem phoneNo1StrItm = new NumeroTelefoneItem("", "");
    private NumeroTelefoneItem phoneNo2StrItm = new NumeroTelefoneItem("", "");
    private RegDetailVO reg = null;

    public DiscarForm() {
        super("", null);
        nomeStrItm.setLabel("Nome: ");
        phoneNo1StrItm.setItemCommandListener(this);
        phoneNo1StrItm.addCommand(dialCmd);
        phoneNo2StrItm.setItemCommandListener(this);
        phoneNo2StrItm.addCommand(dialCmd);
        phoneNo2StrItm.setLayout(Item.LAYOUT_NEWLINE_BEFORE);
        addCommand(cmdBack);
        addCommand(operCmd);
        setCommandListener(this);
    }

    public void commandAction(Command arg0, Displayable arg1) {

        if (arg0.equals(cmdBack)) {
            J2meAppHelper.getInstance().changeForm(new DetalheRegistroForm(), reg);
        } else if (arg0 == operCmd) {
            J2meAppHelper.getInstance().changeForm(new OperadoraForm(), this);
        }
    }

    public void commandAction(Command cmd, Item item) {
        if (cmd == dialCmd && item instanceof NumeroTelefoneItem) {
            ((NumeroTelefoneItem) item).discar();
        }
    }

    public Displayable getDisplyable() {
        return this;
    }

    public void initialize(Object parameter) {

        if (parameter instanceof RegDetailVO) {
            reg = (RegDetailVO) parameter;
        }
        
        deleteAll();

        nomeStrItm.setText(reg.getRegistro().getNome());
        append(nomeStrItm);

        final boolean hasPhoneNo1 = reg.getRegistro().getTelefone().equals("") == false;
        final boolean hasPhoneNo2 = reg.getRegistro().getTelefone2().trim().length() > 0;
        
        phoneLbl.setLabel("Discar: ");
        append(phoneLbl);

        if (hasPhoneNo1) {
            phoneNo1StrItm.setNumero(reg.getRegistro().getTelefone());
            phoneNo1StrItm.setDDD(reg.getRegistro().getDDD().toString());
            append(phoneNo1StrItm);
        }

        if (hasPhoneNo2) {
            phoneNo2StrItm.setNumero(reg.getRegistro().getTelefone2());
            phoneNo1StrItm.setDDD(reg.getRegistro().getDDD().toString());
            append(phoneNo2StrItm);
        }
    }
}